Part 2 - Testing the Stress Meter

You must not start this section until the apparatus has been inspected.

Version 1.0, April 1998

The purpose of this part of the project is to test out the working of the stress meter, and to set up some software for your measurements, so that you can discover how best to collect some data. You also could find out how stable the system is, how much 'noise' there is, and whether the range of readings you can get is adequate.

Software

You are going to use QBASIC for these tests. Microcomputers evolved with various versions of BASIC, an interpreted language which enabled very easy programming of the computer. Debugging programmes is particularly easy since a line-by-line response is given and there is no compilation step. Type 'RUN' and the program goes! Early IBM PCs had a version of BASIC (GW-BASIC) in ROM. Then with DOS 5 and DOS 6, the QBASIC interpreter, QBASIC.EXE was provided alongside of the operating system. Another slightly different version of BASIC, QuickBasic, is compiled, and came before QBASIC and was then sold alongside QBASIC. QBASIC and QuickBasic have now been withdrawn and replaced by VisualBasic (which you met briefly in the 2nd Excel exercise), but the former two can still be used from Windows. The VisualBasic compiler (for DOS) will compile any QBASIC or QuickBasic source code. One aspect of BASIC is that it has very useful functions enabling I/O at a low level of the computer, examples were PEEK, POKE, IN and OUT and others, the particular functions depended on the various input and output devices and the microcomputer system.

It is not our purpose to teach you another computer language, it is just that QBASIC still provides a very convenient and easy-to-use method of testing any interface. It is also quite adequate for data collection in this simple type of equipment, and it is also far easier to use than a C programme. Once you have a working stress meter with a QBASIC programme to collect data, then try to translate Programme 3 of the three QBASIC programmes into Borland C++. This conversion to C++, counts for 20% of the project mark, it may be omitted if you (and your partner) are prepared to forego these marks and just use the QBASIC programmes provided. If you provide some significant improvements to the QBASIC, then extra marks will also be awarded. Any programmes must be documented properly, and working (compiled if C++) programmes presented on your floppy disk.

Testing

The following three QBASIC programmes provide tests for your meter, try each of these since they have varying facility. You will need to type them in before using them. (Our thanks to Tony Makepeace for the QBASIC programming suggestions.)

Type in, and store on your disk(s), the following programme. In this QBASIC programme : REM is a comment; CLS clears the screen (window); skinres is a variable into which the data is read via the STICK(0) function; PRINT writes this on the screen; line 50 reads a buffer to see if a key on the keyboard has been typed, if it has then the length of the string a$ will be 1, so the programme ends (exits) from line 60; lines 70/80 is an example of a software delay loop; on line 100 you have the infamous GOTO statement, to go back to Line 30 for another reading. Very useful here!

Programme 1

   10  REM Biofeedback programme 1
   20  CLS
   30  skinres = STICK(0)
   40  PRINT skinres
   50  a$ = INKEY$
   60  IF LEN(a$) = 1 THEN END
   70  FOR delay = 1 TO 2000
   80  NEXT delay
   90  CLS
   100  GOTO 30

The game port on the PC is a 15-way female connector. Attach your stress meter to the computer via this connection, run the programme. You may need to change the delay to either speed up or slow down the rate at which readings appear on the screen. Test the stress meter in open circuit, with the electrodes touching each other, and then attached to fingers. You may need to adjust the potentiometer VR1 so that the skin resistance readings are on-scale. The meter may function better if the readings are kept at lower values (<180). Now repeat the tests with Programme 2, a slightly more user-friendly version. However note that the line numbers have been removed, and the GOTO replaced by a DO...LOOP statement. Go through the programme code and make sure you understand what it is doing.{This was also explained for you during a lecture}

Programme 2

   REM Biofeedback programme 2
   CLS
   PRINT "Press any key to end the programme"
   LOCATE 10, 23
   PRINT "Skin Resistance = "
   DO
      skinres = STICK(0)
      LOCATE 10, 40
      PRINT USING "####" STICK(0) 'Skinres on game port
      timex = TIMER + .2 'Delay between readings in seconds
      DO
        IF INKEY$ > "" THEN END
      LOOP UNTIL TIMER > timex
   LOOP
The next programme, Programme 3, enables the data readings to be stored in the form of a text file on a floppy disk, the file is called A:/BIOTEST.PRN. Go through the programme and work out what the different statements are doing - the code is quite readable, and there are already some comments (behind the ' marks on code lines).

Programme 3

   REM Biofeedback programme 3
   INPUT "Enter time between readings in seconds ", t
   CLS
   OPEN "A:/BIOTEST.PRN" FOR APPEND AS #1 'Create text output file on floppy disc
   start = TIMER
   timex = TIMER
   PRINT "Press any key to end the programme"
   LOCATE 10, 23
   PRINT "Skin Resistance = "
   DO
     skinres% = STICK(0) 'reads game port into variable skinres%
     LOCATE 10, 40
     PRINT USING "####"; STICK(0); 'Skinres on game port
     PRINT USING "####.##"; TIMER - start;
     PRINT " seconds"
     PRINT #1, USING "####.##"; TIMER - start;
     PRINT #1, ",";
     PRINT #1, USING "####"; skinres%
     timex = timex + t 'Delay between readings in seconds
     DO
       IF INKEY$ > "" THEN END
       LOOP UNTIL TIMER > timex
   LOOP
If you want to store other files, for instance to compare data, then obviously you need to change the file name. If you store data on the hard disk you will need to specify a directory (and path), and also transfer it to your floppy disk when you finish. Please don't leave it on the Tiger lab hard disks.

An Alternative to the STICK(0) Function ?

If you consult a PC Port Memory map you will discover that the eight port hexadecimal addresses &H200 to &H207 have been set aside for input from the Games/Joystick device. Port &H201 is used for the 'fire buttons', with bits 4 to 8 used respectively, bits 0-3 are unused. So, which of the addresses &H200, &H202 to &H207 are used as analogue inputs from the potentiometers? The function INP(Port_Address) in QBASIC reads from a chosen Port_Address. Thus by substituting INP(Port_Address) for STICK(0) in Programme 2 we can try to read the data from a port, and if the stress meter is attached, we should get the same reading as that provided by STICK(0). By trying the port addresses in the address range &H200 to &H207 for the Game/Joystick port, can you discover which is the appropriate one for your stress meter? You will need to substitute the decimal equivalents of the address into the INP function.

{Note that the corresponding function to outputs a byte, Data, from the computer to a particular port at Port_Address is OUTP(Port_Address, Data)}

Borland C++ 4.5, BC++

Once the BASIC Programme 3 is working you can convert it into a simple C program to do the same thing. Using the BC++ system is much more complicated than using QBASIC, to start with a compilation step is needed since BC++ is compiled. Another order of magnitude of complexity enters if we want to put the program into a proper Windows environment. The purpose of this section is to introduce you to using C for low level input (or output), not to teach Windows programming.

Hello World

Start BC++ by double clicking on the icon. A blank window will appear with the menu items : File, Edit, Search, View, Project, Debug, Tool, Options,Window, Help. BC++ is organised into Projects comprising a number of files, the Projects have .IDE suffixes.

You should set up your programs on your floppy disk. Choose Options, then Project.. and set the Source Directory to A:\ , if you don't have a floppy, set up a folder for yourself on C: as C:\<your initials>, remove this when you finish the exercises. Choose the Project menu, Select the New Project command. Enter hello.ide in the edit box requesting the project path and name. The dialog box also shows the name "hello" in the target name edit box. Also choose the Target type as EasyWin[.exe]

Click Advanced. Select the check box labelled .cpp Node. Ensure that the two check boxes labelled .rc and .def are unchecked. Click OK to create the new project file.

Click the hello.exe node to view the hello.cpp node (your source code file). Double click on this node to start the IDE editor. Enter a suitable program e.g.

   // Yet another "hello world" program
   #include <iostream.h>
   main()
   {
      cout << "Hello Tiger World!!";
      return 0;
   }
Choose the Save As.. command in the File menu. save your program as Hello.cpp on A:\ or the C:\<your initials>

Ctrl+F9 will compile, link and run the hello.exe program. This should (hopefully) introduce you to the programming environment for BC++. The Help menu is there for you to use if necessary, also the Borland C++ CDROM is available to view from the Tiger Server.

Whello is the Windows version of a 'Hello World' program, it can be found set up as a project, whello.ide, buried down in the directory : C:\ ..\examples\windows\whello. If you open up the whello.cpp program in the editor you can see that just setting up a 'hello world' type of program in BC++ (this is a C++ program now) is several orders of magnitude more complex than your previous experiences of programming, it has 275 lines of code. The complexity appears because we now need to control the Windows environment from our program. BC++ comes with a good selection of other ready-made programs for you to study, you don't need these today, but if you intend taking your programming skills further, then try running some of them at another time. One of these is a Mahjong game program. They provide excellent examples to illustrate C++ programming. A suitable book is 'Teach Yourself Borland C++ 4.5 in 21 Days", by N Shammas, C Arnush and E.Mulroy, Sams, 1995. There are also more recent releases of Borland C++ than 4.5.

Converting the BASIC Stress Project Programme into BC++

In order to get data from the Games Port you need to know how to find the equivalent of the BASIC STICK(0) function, or to write an equivalent function for yourself, in any case your experience of C should tell you that you need a suitable C library call. However the Games/Joystick port is almost a forgotten part of the modern PC, so that you are unlikely to find an equivalent of STICK in most C systems. We probably need to read the port in a different fashion.

In BC++ the library conio.h provides two functions inp(Port_address) and outp(Port_address, data) to input and output bytes of data from any input/output device at an address Port_address. Thus in BC++ line 30 of Programme 1 becomes

skinres = inp(Port_Address_Potentiometer_1)

You should have most of the information needed to write a BC++ programme, you can assume that the BC++ compiler supports the ANSI C libraries you met during the C programming course during the first part of COMS12301. You can use the same method for writing, compiling and running the programme that was used with the Hello programme.